package com.example.videoserver.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.example.videoserver.entity.Button;
import com.example.videoserver.entity.VideoEntity;
import com.example.videoserver.utli.ResponseRest;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/button")
@RestController()
public class ButtonController {

    private final String filePath = "/config/button.properties";

    @PostMapping("/save")
    public ResponseRest save(@RequestBody Button videoEntity){
        videoEntity.setId(UUID.fastUUID().toString());
        System.out.println(videoEntity);

        FileReader reader = new FileReader(filePath);
        System.out.println(reader.readString());

        List<Button> buttonList = JSONUtil.toList((reader.readString()), Button.class);

        buttonList.add(videoEntity);

        FileWriter writer = new FileWriter(filePath);
        writer.write(JSONUtil.toJsonStr(buttonList));


        return ResponseRest.success();
    }

    @GetMapping("/query")
    public Object query(@RequestParam String videoId) {
        if (!FileUtil.exist(filePath)) {
            FileUtil.touch (filePath);
            FileWriter writer = new FileWriter(filePath);
            writer.write("[]");
        }
        FileReader reader = new FileReader(filePath);
        System.out.println(reader.readString());

        List<Button> buttonList = JSONUtil.toList((reader.readString()), Button.class);

        List<Button> list = new ArrayList<>();

        for (Button button :
                buttonList) {

            if (ObjectUtil.isNull(button.getVideoId())) {
                continue;
            }

            if (button.getVideoId().equals(videoId)) {
                list.add(button);
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", list.size());
        map.put("data", list);

        return map;
    }

    @DeleteMapping("/delete")
    public Object del(@RequestParam("id") String id) {
        System.out.println(id);

        FileReader reader = new FileReader(filePath);
        System.out.println(reader.readString());

        List<Button> buttonList = JSONUtil.toList((reader.readString()), Button.class);
        buttonList.removeIf(button -> button.getId().equals(id));



        FileWriter writer = new FileWriter(filePath);
        writer.write(JSONUtil.toJsonStr(buttonList));;
        return ResponseRest.success();
    }

    @PutMapping("/update")
    public ResponseRest update(@RequestBody Button videoEntity){

        System.out.println(videoEntity);

        FileReader reader = new FileReader(filePath);
        System.out.println(reader.readString());

        List<Button> buttonList = JSONUtil.toList((reader.readString()), Button.class);
        buttonList.removeIf(button -> button.getId().equals(videoEntity.getId()));
        buttonList.add(videoEntity);

        FileWriter writer = new FileWriter(filePath);
        writer.write(JSONUtil.toJsonStr(buttonList));


        return ResponseRest.success();
    }

    @GetMapping("/getScript")
    public Object getButton(@RequestParam String videoId, @RequestParam Integer time) {
        FileReader reader = new FileReader(filePath);
        List<Button> buttonList = JSONUtil.toList((reader.readString()), Button.class);

        List<Button> list = new ArrayList<>();

        for (Button button :
                buttonList) {

            if (ObjectUtil.isNull(button.getVideoId()) || ObjectUtil.isNull(button.getStartTime())) {
                continue;
            }

            if (button.getVideoId().equals(videoId) && button.getStartTime().equals(time)) {
                list.add(button);
            }
        }

        return ResponseRest.success(list);
    }
}
